package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanglibao.woodtrade.woodtrade.MyApplication;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.util.CommonUtil;

import butterknife.Bind;

/**
 * Created by lugg on 2016/4/5.
 * 所有子页面的xml 必须 include commotTitle
 * 如果不要显示title 使用RelativeLayout的setVisibility(View.GONE)隐藏
 * tvTitle.settext()设置title显示文字
 * setRightText()设置title右边的按钮文字 并显示右边的按钮
 *
 * 使用addViewClicklistener()设置 点击事件
 */
public class BaseActivity extends Activity implements View.OnClickListener{

    MyApplication myApplication;

    @Bind(R.id.tv_commonTitle_title)
    public TextView tvTitle;

    @Bind(R.id.imgbtn_commonTitle_back)
    public ImageButton tvBack;

    @Bind(R.id.tv_commonTitle_right)
    public TextView tvRight;

    @Bind(R.id.rl_common_title)
    RelativeLayout rl_common_title;

    public int dip2px ;

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        myApplication = (MyApplication) getApplication();
        myApplication.addActivity(this);
        dip2px = CommonUtil.dip2px(this, 8);
    }

    public MyApplication getMyApplication(){
        if (myApplication == null){
            myApplication = (MyApplication) getApplication();
        }
        return myApplication;
    }

    public void setRightText(String text){
        tvRight.setText(text);
        tvRight.setVisibility(View.VISIBLE);
    }
    public void setRightText(int strID){
        String string=this.getResources().getString(strID).toString();
        setRightText(string);
    }
    
    

    public void addViewClicklistener(View... views){
        for (int i = 0; i < views.length; i++){
            views[i].setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.imgbtn_commonTitle_back){
            onBackPressed();
        }
    }
    public void MyTransalteActivity(Class activityClass){
        startActivity(new Intent(this,activityClass));
        this.overridePendingTransition(R.anim.enter_in,R.anim.enter_out);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        this.overridePendingTransition(R.anim.extre_in,R.anim.extre_out);
    }
}
